package com.wdg.util;

import java.io.InputStream;
import java.lang.annotation.Retention;
import java.sql.*;
import java.util.Properties;

public class DBUtil {

    public static Connection getCn(){
        Connection cn=null;
        Properties pro=new Properties();
        InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            pro.load(inputStream);
            String driver = pro.getProperty("jdbc.driver");
            String url = pro.getProperty("jdbc.url");
            String username = pro.getProperty("jdbc.username");
            String password = pro.getProperty("jdbc.password");

            Class.forName(driver);
            cn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cn;
    }
    public static void release(ResultSet rs, Statement st, Connection conn) {
        closeRs(rs);
        closeSt(st);
        closeConn(conn);
    }

    public static void closeConn(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            conn = null;
        }
    }

    public static void closeRs(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            rs = null;
        }
    }

    public static void closeSt(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            st = null;
        }
    }
    public static void closePst(PreparedStatement pst) {
        try {
            if (pst != null) {
                pst.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            pst = null;
        }
    }
}
